package com.android.volley.codec;



import android.util.Base64;

import java.security.MessageDigest;

public abstract class Coder {

	protected static final byte[] PREFIX = "SECRET".getBytes();
	private static final String KEY_SHA = "SHA";
	private static final String KEY_MD5 = "MD5";

	/**
	 * BASE64解密
	 *
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptBASE64(String key) throws Exception {

		return Base64.decode(key, Base64.DEFAULT);
	}

	public static byte[] decryptBASE64Byte(byte[] data) throws Exception {

		return Base64.decode(data, Base64.DEFAULT);
	}

	/**
	 * BASE64加密
	 *
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encryptBASE64(byte[] key) throws Exception {

		return Base64.encodeToString(key, Base64.DEFAULT);
	}

	public static byte[] encryptBASE64Byte(byte[] data) throws Exception {

		return Base64.encode(data, Base64.DEFAULT);
	}
	/**
	 * MD5加密
	 *
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptMD5(byte[] data) throws Exception {

		MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
		md5.update(data);

		return md5.digest();

	}

	/**
	 * SHA加密
	 *
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptSHA(byte[] data) throws Exception {

		MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
		sha.update(data);

		return sha.digest();
	}
}